﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;

namespace MPBlog.Core
{
    public static class Utils
    {
        private static string relativeWebRoot;

        /// <summary>
        /// 返回网站根目录的绝对路径,以/结尾
        /// </summary>
        public static Uri AbsoluteWebRoot
        {
            get
            {
                var context = HttpContext.Current;
                if (context == null)
                {
                    throw new WebException("The current HttpContext is null");
                }

                var absoluteurl = context.Items["absoluteurl"];
                if (absoluteurl == null)
                {
                    absoluteurl = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority) + RelativeWebRoot);
                    context.Items["absoluteurl"] = absoluteurl;
                }

                return absoluteurl as Uri;

            }
        }

        /// <summary>
        /// 返回网站根目录的相对路径,以/结尾
        /// </summary>
        public static string RelativeWebRoot
        {
            get
            {
                return relativeWebRoot ??(relativeWebRoot = VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings["VirtualPath"]));
            }
        }

        /// <summary>
        /// 获得当前请求所在目录的绝对路径，以/结尾
        /// </summary>
        public static string CurrentFolderAbsolutePath
        {
            get 
            {
                var context = HttpContext.Current;
                if (context == null)
                {
                    throw new WebException("HttpContext is null");
                }
                string url = context.Request.Url.ToString();
                string path = url.Substring(0, url.LastIndexOf('/')+1);
                return path;
            }
        }

        /// <summary>
        /// 根据相对路径获得绝对路径
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        public static string GetAbsolutePath(string virtualPath)
        {
            if(virtualPath == "#" || string.IsNullOrEmpty(virtualPath))
            {
                return virtualPath;
            }
            return VirtualPathUtility.ToAbsolute(virtualPath);
        }

        /// <summary>
        /// 弹出页面顶部的信息提示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg"></param>
        /// <param name="messageType"></param>
        /// <param name="autoCloseTime"></param>
        public static void Message(this Page page,string msg,MessageType messageType,int autoCloseTime)
        {
            string msgScript = string.Format("MPBlog.utils.message('{0}','{1}',{2});", msg, messageType, autoCloseTime);
            ScriptManager.RegisterStartupScript(page, page.GetType(), "MPBlogMessage", msgScript, true);
        }


        /// <summary>
        /// 顶部信息提示的类型
        /// </summary>
        public enum MessageType
        {
            /// <summary>
            /// 消息
            /// </summary>
            info,
            /// <summary>
            /// 成功
            /// </summary>
            success,
            /// <summary>
            /// 错误/失败
            /// </summary>
            error
        }
    }
}
